package com.miku.project.service.impl.inner;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.miku.mikuapicommon.model.entity.User;
import com.miku.mikuapicommon.model.vo.UserVO;
import com.miku.mikuapicommon.service.InnerUserService;
import com.miku.project.common.ErrorCode;
import com.miku.project.exception.BusinessException;
import com.miku.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

@DubboService
@Slf4j
public class InnerUserServiceImpl implements InnerUserService {

    @Resource
    private UserService userService;
    /**
     * 通过访问密钥获取invoke用户
     * 按凭证获取invoke用户
     *
     * @param accessKey 访问密钥
     * @return {@link UserVO}
     */
    @Override
    public UserVO getInvokeUserByAccessKey(String accessKey) {
        if (StringUtils.isAnyBlank(accessKey)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"accessKey不能为空");
        }
        //按凭证获取invoke用户
        User user = userService.getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getAccessKey, accessKey));
        if (user == null){
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return userVO;
    }
}
